package com.zesionlee.server.config

import com.zesionlee.server.entity.DBEntity
import com.zesionlee.server.exception.CustomException
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.datasource.DriverManagerDataSource

class DataSource(dbEntity: DBEntity) {
    private var jdbcTemplate = JdbcTemplate()

    init {
        val dataSource = DriverManagerDataSource()
        dataSource.setDriverClassName("org.postgresql.Driver")
        dataSource.url = "jdbc:postgresql://${dbEntity.host}:${dbEntity.port}/${dbEntity.database}?"
        dataSource.username = dbEntity.username
        dataSource.password = dbEntity.password
        jdbcTemplate = JdbcTemplate(dataSource)
        println("jdbc init")
    }

    fun test(): String {
        try {
            val list = jdbcTemplate.queryForList("SELECT table_name FROM information_schema.tables")
            return "success"
        } catch (e: Exception) {
            return e.message!!
        }
    }


}